<?php
include 'score_lib.php';

if ($s_Action == 'update-score') {
    
    // <editor-fold defaultstate="collapsed" desc="Update-Score">
    get_passport_module_action($db_conn, $s_PPO_ModuleID, $s_PPO_ActionID, $a_filterings);
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    db_delete_row($db_conn, 'user_score', "userid = '$f_userid' and data = '$f_data' and scoretype = '$f_scoretype'");

    // <editor-fold defaultstate="collapsed" desc="Write Log">
    $a_insert_row = array (
        'userid'     => $f_userid,
        'type'       => 'HOC_LUC',
        'data'       => $f_data,
        'scoretype'  => $f_scoretype,
        'score'      => $f_score,
        'note'       => $f_note,
        'updaterid'  => $s_UserID,
        'updatedate' => $f_updatedate,
    );

    db_insert_row($db_conn, $a_insert_row, 'user_score_log');
    // </editor-fold>
    
    if ($f_score) {
        
        $a_insert_row = array(
            'userid'    => $f_userid,
            'type'      => 'HOC_LUC',
            'data'      => $f_data,
            'scoretype' => $f_scoretype,
            'score'     => $f_score,
            'date'      => date('Y-m-d'),
        );

        db_insert_row($db_conn, $a_insert_row, 'user_score');
    }

    $v_response_json->code = 200;
    $v_response_json->data = array(
        'scoretype'  => $f_scoretype,
        'scorevalue' => $f_score,
        'userid'     => $f_userid,
        'data'       => $f_data,
    );
    echo json_encode($v_response_json, true);
    
    // <editor-fold defaultstate="collapsed" desc="Recalculate for this User's class">
    ob_start();
    $db_query = "
        select  course, team, level, crew
        from    user_class
        where   userid = '$f_userid' and course = '" . $a_KhoaHoc_Info['start_year'] . "'
        limit   1";
    
    $db_result = db_query($db_conn, $db_query);
    
    $a_option = array();

    if ($d_row = db_fetch($db_result)) {
        
        $a_option = array (
            'course'         => $d_row->course,
            'team'           => $d_row->team,
            'level'          => $d_row->level,
            'crew'           => $d_row->crew,
            'user_condition' => $s_get_THIEU_NHI_condition,
        );
    }
    
    set_user_rank($a_option, $a_KhoaHoc_Info, $a_Loai_ChuyenCan, $db_conn);
    ob_clean();
    // </editor-fold>
    
    exit;

    // </editor-fold>
}
else {
    raise_error('Bạn chưa được cấp quyền thực hiện chức năng này');
}
?>
